#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e5 + 10, M = 320;

int n, m;
int block, t;
int st[M], ed[M], pos[N];
int col[N];
int cnt[M][N];

struct tree
{
    ll v[N];
    int lowbit(int x) { return x & -x; }
    void add(int x, ll y) {
        for (; x <= n; x += lowbit(x))
            v[x] += y;
    }
    ll ask(int x) {
        ll ret = 0;
        for (; x; x -= lowbit(x))
            ret += v[x];
        return ret;
    }
} tr;

void init()
{
    block = sqrt(n);
    t = (n + block - 1) / block;
    for (int i = 1;i <= t;i++) {
        st[i] = (i - 1) * block + 1;
        ed[i] = i * block;
    }
    ed[t] = n;
    for (int i = 1;i <= n;i++) pos[i] = (i - 1) / block + 1;
    for (int i = 1;i <= t;i++)
        for (int j = st[i];j <= ed[i];j++)
            cnt[i][col[j]]++;
}

inline int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch>'9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    return x * f;
}

void write(ll x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
    return;
}

void solve()
{
    n = read(), m = read();
    for (int i = 1;i <= n;i++) col[i] = read();
    for (int i = 1;i <= n;i++) tr.add(i, read());
    init();
    int op, x, k;
    while (m--) {
        op = read(), x = read(), k = read();
        if (op == 1) {
            int p = pos[x];
            cnt[p][col[x]]--;
            cnt[p][k]++;
            col[x] = k;
        } else if (op == 2) {
            ll odd = tr.ask(x) - tr.ask(x - 1);
            tr.add(x, k - odd);
        } else {
            vector<int> sum(t + 1);
            vector<bool> vis(n + 1);
            for (int i = 1;i <= k;i++) {
                int c = read(); vis[c] = 1;
                for (int j = 1;j <= t;j++) sum[j] += cnt[j][c];
            }

            int l = x, r = x;
            for (int i = pos[l];i >= 1 && sum[i] == ed[i] - st[i] + 1;i--) l = st[i];
            for (int i = pos[r];i <= t && sum[i] == ed[i] - st[i] + 1;i++) r = ed[i];

            for (int i = l - 1;i >= 1 && vis[col[i]];i--) l = i;
            for (int i = r + 1;i <= n && vis[col[i]];i++) r = i;

            write(tr.ask(r) - tr.ask(l - 1));putchar('\n');
        }
    }

    for (int i = 0;i <= n;i++) tr.v[i] = 0;
    for (int i = 1;i <= t;i++) fill(cnt[i] + 1, cnt[i] + n + 1, 0);
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    T = read();
    for (int i = 1;i <= T;i++) {
        solve();
    }
}